#include "ZoomButton.h"
#include "utils_qt.h"
#include "Config.h"
using namespace Config; // 引入命名空间

#include <QMouseEvent>
#include <QThread>
#include <QCursor>

#include <iostream>
#include <string>

ZoomButton::ZoomButton(QWidget *parent, std::string name, bool W_flag, bool H_flag)
    : QPushButton(parent), _name(name), _parent(parent), _w_flag(W_flag), _h_flag(H_flag)
{
    // 初始化代码（如果需要）
    // 设置按钮的鼠标悬浮光标
    if (this->_name == "R")
        this->setCursor(Qt::SizeHorCursor); // 更改光标
    if (this->_name == "L")
        this->setCursor(Qt::SizeHorCursor); // 更改光标
    if (this->_name == "D")
        this->setCursor(Qt::SizeVerCursor); // 更改光标
    if (this->_name == "RD")
        this->setCursor(Qt::SizeFDiagCursor); // 更改光标
    if (this->_name == "LD")
        this->setCursor(Qt::SizeBDiagCursor); // 更改光标
}

// 定义信号
void ZoomButton::mousePressEvent(QMouseEvent *event)
{

    if (event->button() == Qt::LeftButton)
    {
        m_dragging = true;
        m_lastPos = QCursor::pos();
        // std::cout << " -------------> m_lastPos " << m_lastPos.x() << " " << m_lastPos.y() << std::endl;
        event->accept();
    }
}

void ZoomButton::mouseMoveEvent(QMouseEvent *event)
{
    if (m_dragging)
    {
        QPoint globalMousePos = QCursor::pos();

        // 计算拖动的距离
        int deltaX = _w_flag ? globalMousePos.x() - m_lastPos.x() : 0;
        int deltaY = _h_flag ? globalMousePos.y() - m_lastPos.y() : 0;
        // std::cout << "-> " << deltaX << " " << deltaY << std::endl;
        // 输出信号
        emit sizeChanged(this->_name, deltaX, deltaY);

        m_lastPos = globalMousePos; // 更新最后的位置
    }
}

void ZoomButton::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        m_dragging = false;
        event->accept();
    }
}

void ZoomButton::SetDay()
{
    this->setStyleSheet(ZoomButtonQss(ZoomButtonDayColor, ZoomButtonLength / 2));
}

void ZoomButton::SetNight()
{
    this->setStyleSheet(ZoomButtonQss(ZoomButtonDayColor, ZoomButtonLength / 2));
}

ZoomButton::~ZoomButton()
{
    // 清理代码（如果需要）
}